package com.joven.shelltest.utils.functionifelse;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Author wzj
 * @Date 2023-11-07 15:17
 **/
public class RuleFactory {

    private static final Map<Predicate<Cart>, Function<Cart, Double>> RULE_MAP = new HashMap<>();

    static {
        RULE_MAP.put(CartPredicate.VALUE_SIX, CartFunction.ONE_POINT_FIVE);
        RULE_MAP.put(CartPredicate.VALUE_SEVEN, CartFunction.TWO_POINT_FIVE);
        RULE_MAP.put(cart -> cart.getValue() == 5, cart -> cart.getValue() * 2);
    }

    public static Double getRuleDealValue(Cart cart) {
        return RULE_MAP.entrySet().stream()
                .filter(entry -> entry.getKey().test(cart))
                .map(Map.Entry::getValue)
                .findFirst().orElse(CartFunction.DEFAULT)
                .apply(cart);
    }

}
